package com.chhuang.utils.image;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.Locale;
import java.util.UUID;

import javax.imageio.IIOImage;
import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
import javax.imageio.ImageWriteParam;
import javax.imageio.ImageWriter;
import javax.imageio.metadata.IIOMetadata;
import javax.imageio.stream.ImageInputStream;
import javax.imageio.stream.ImageOutputStream;

import org.apache.commons.io.FileUtils;

import com.github.jaiimageio.plugins.tiff.TIFFImageWriteParam;

public class ImageUtil {
	/**
	 * 图片文件转换为tif格式
	 * 
	 * @param imageFile
	 *            文件路径
	 * @param imageFormat
	 *            文件扩展名
	 * @return
	 */
	public static File createImage(File imageFile, String imageFormat) {
		File tempFile = null;
		try {
			Iterator<ImageReader> readers = ImageIO.getImageReadersByFormatName(imageFormat);
			ImageReader reader = readers.next();

			ImageInputStream iis = ImageIO.createImageInputStream(imageFile);
			reader.setInput(iis);
			// Read the stream metadata
			IIOMetadata streamMetadata = reader.getStreamMetadata();

			// Set up the writeParam
			TIFFImageWriteParam tiffWriteParam = new TIFFImageWriteParam(Locale.CHINESE);
			tiffWriteParam.setCompressionMode(ImageWriteParam.MODE_DISABLED);

			// Get tif writer and set output to file
			Iterator<ImageWriter> writers = ImageIO.getImageWritersByFormatName("tiff");
			ImageWriter writer = writers.next();

			BufferedImage bi = reader.read(0);
			IIOImage image = new IIOImage(bi, null, reader.getImageMetadata(0));
			tempFile = tempImageFile(imageFile);
			ImageOutputStream ios = ImageIO.createImageOutputStream(tempFile);
			writer.setOutput(ios);
			writer.write(streamMetadata, image, tiffWriteParam);
			ios.close();

			writer.dispose();
			reader.dispose();

		} catch (IOException e) {
			e.printStackTrace();
		}
		return tempFile;
	}

	private static File tempImageFile(File imageFile) {
		StringBuffer path = new StringBuffer(imageFile.getParent());
		String tempImageName = UUID.randomUUID().toString();
		path.append("/").append(tempImageName).append(".tif");
		return new File(path.toString());
	}
	

    /**
     * 获取图片类型
     * @param imageFile
     * @return
     * @throws IOException 
     */
    public static String getImgType(File imageFile) throws IOException {  
    	byte[] data = FileUtils.readFileToByteArray(imageFile);
        String type = "";  
        // JPG test:  
        if (data[6] == 'J' && data[7] == 'F' && data[8] == 'I' && data[9] == 'F') {  
            type = "jpg";  
            return type;  
        }
        // Png test:  
        if (data[1] == 'P' && data[2] == 'N' && data[3] == 'G') {  
            type = "png";  
            return type;  
        }  
        // Gif test:  
        if (data[0] == 'G' && data[1] == 'I' && data[2] == 'F') {  
            type = "gif";  
            return type;  
        }  
        return type;  
    }
}